package com.sunni.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunni.controller.AjaxRes;
import com.sunni.entity.Employee;
import com.sunni.entity.PageListRes;
import com.sunni.entity.QueryVo;
import com.sunni.entity.Role;
import com.sunni.mapper.EmployeeMapper;
import com.sunni.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageListRes selectAll(QueryVo qv) {

        /*查询总页数、员工信息List*/
        Page<Object> page = PageHelper.startPage(qv.getPage(), qv.getRows());
        List<Employee> employeeList = employeeMapper.selectAll(qv);

        /*封装PageBean*/
        PageListRes pageListRes = new PageListRes();
        pageListRes.setTotal(page.getTotal());
        pageListRes.setRows(employeeList);
        return pageListRes;
    }

    @Override
    public AjaxRes addEmployee(Employee employee) {
        System.out.println("------------EmployeeService-----添加数据---"+employee+"----------------------");

        AjaxRes ajaxRes = new AjaxRes(false,"添加失败");

        try {
            /*1. 添加 employee*/
         employeeMapper.insert(employee);
         /*2. 把employee的角色  添加进employee_role_rel表*/
        if (employee.getRoleList()!=null && employee.getRoleList().size()>0){
            for (Role role : employee.getRoleList()) {
                employeeMapper.insertEmployeeRoleRel(employee.getId(),role.getRid());
            }
        }
         ajaxRes.setSuccess(true);
         ajaxRes.setMsg("添加成功");
        }catch (Exception e){

        }

        return ajaxRes;
    }

    @Override
    public void updateEmployee(Employee employee) {

        /* 修改employee表 员工信息 */
        employeeMapper.updateByPrimaryKey(employee);

        /*修改employee表、role表 的多对多映射关系 (修改employee_role_rel表)*/
        /*1.删除 原来的映射关系*/
        employeeMapper.deleteEmployeeRoleRelByEid(employee.getId());
        /*2.添加 新的映射关系 （如果 roleList不为空的话，，，说明这个employee有role，，就执行insert 添加新的映射关系）*/
        if (employee.getRoleList()!=null && employee.getRoleList().size()>0){
            for (Role role : employee.getRoleList()) {
                employeeMapper.insertEmployeeRoleRel(employee.getId(),role.getRid());
            }
        }
    }

    @Override
    public void updateState0(Long id) {
        employeeMapper.updateState0(id);
    }

    /*员工页面，编辑，角色下拉框 数据回显*/
    @Override
    public List<Long> selectRolesByEid(Long eid) {
        return employeeMapper.selectRolesByEid(eid);
    }

    /*login登录 ，Cintroller/realm/EmployeeRealm，根据username查询有木有这个员工*/
    @Override
    public Employee selectEmployeeByUsername(String username) {
        return employeeMapper.selectEmployeeByUsername(username);
    }

    /*登录页面  @RequiresPermissions("employee:index")-->EmployeeRealm授权方法 根据eid查询所有role name*/
    @Override
    public List<String> selectRoleNamesByEid(Long id) {
        return employeeMapper.selectRoleNamesByEid(id);
    }

    /*登录页面 EmployeeController @RequiresPermissions("employee:index")-->EmployeeRealm授权方法 根据eid查询所有permission presource*/
    @Override
    public List<String> selectPermissionResourcesByEid(Long id) {
        return employeeMapper.selectPermissionResourcesByEid(id);
    }

}
